﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Xiami.util {
	class XTools { 

        /// <summary>
        /// 新建文件夹
        /// </summary>
        /// <param name="dirPath">string</param>
        /// <returns></returns>
        public static System.IO.DirectoryInfo mkdir(string dirPath) {
            return System.IO.Directory.CreateDirectory(dirPath);
        }

        /// <summary>
        /// 给生成合法的目录路径
        /// </summary>
        /// <param name="dirPath">string</param>
        /// <returns></returns>
        public static string genValidDirPath(string dirPath) {
            if(true == XVerify.isValidDirPath(dirPath)) {
                return dirPath;
            }

            return dirPath + System.IO.Path.DirectorySeparatorChar;
        }

        /// <summary>
        /// 是到文件 
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string getFileFormat(string filePath) {
            return filePath.Substring(filePath.LastIndexOf('.'));
        }

        /// <summary>
        /// 得到文件的名称
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string getFileName(string filePath) {
            return filePath.Substring(filePath.LastIndexOf("\\") + 1, filePath.LastIndexOf('.') - 1);
        }

        /// <summary>
        /// 得到含有文件格式的文件名
        /// </summary>
        /// <param name="filePath">string</param>
        /// <returns></returns>
        public static string getFileNameHasFormat(string filePath) {
            return filePath.Substring(filePath.LastIndexOf("\\") + 1);
        }

        /// <summary>
        /// 格式化文件名称，如去掉[1]数组下标，将HTML的encode变成正常的字符
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string formatIeTempFileName(string fileName) {
            Regex arrayFileMode = new Regex(@"\[\d+\]");
            fileName = XTools.urlDecode(fileName);  
            return arrayFileMode.Replace(fileName, "");   
        }

        /// <summary>
        /// 链接串的转码如：“”->“%20”空格
        /// </summary>
        /// <returns></returns>
        public static string urlEncode(string str) {
            int loc     = 0;
            StringBuilder strBuf = new StringBuilder();
            Byte[] curStr = System.Text.Encoding.UTF8.GetBytes(str);
            for (loc = 0; loc < curStr.Length; loc ++ ) {
                strBuf.Append(@"%" + Convert.ToString(curStr[loc], 16));
            }
            return "";
        }


        /// <summary>
        /// 链接的反转码如：“%20”->“ ”空格
        /// </summary>
        /// <param name="urlString"></param>
        /// <returns></returns>
        public static string urlDecode(string urlString) {
            //这里还没整好怎么弄.. Convert.ToChar();
            return urlString;
        }

	}
}
